//------------------------------------------------------------------------------
// Libary             : For AJAX
// Create	          : 2007.06.02 By ThanhTX  - SMC
// Vesrion			  : 0.2	
//------------------------------------------------------------------------------
// Date(yyyy.mm.dd)   Developer                       Action
//------------------------------------------------------------------------------
// 2007.06.02         ThanhTX                      Created
//------------------------------------------------------------------------------
// 2007.06.14		  ThanhTX					  Update	
//------------------------------------------------------------------------------
// 2007.06.20		  ThanhTX					  Update back 
//------------------------------------------------------------------------------
// Day la thu vien su ly AJAX cau Sao Mai, ban quyen thuoc ve Cty Sao Mai.
// Moi su su dung va sao chep deu phai duoc phep cua Sao Mai.
//==============================================================================

var debugAjax = false;
var backFlag =false;			// for back
//==============================================================================
// ThanhTX add 2007.06.20
function onFlagBack(){			
	var objIframe  =	getById("iframeBack");
	objIframe.src  = "../run/processebackNo.html?back=ok";
	backFlag = true;
	//alert(backFlag);
	return true;
}
function offFlagBack(){
	backFlag = false;
	objIframe.src  = "../run/processeback.html";
	//alert(backFlag);
	return true;			
}
//==============================================================================

function smc_XmlHttpSend(url,method,params,nameFunctionProcess,noCache) {
	//alert ("smc_XmlHttpSend");
	return smc_XmlHttpRequest(url,method,params,nameFunctionProcess,noCache,false) ;
	
}

function smc_XmlHttpRequest(url,method,params,nameFunctionProcess,noCache,show) {
	//alert ("smc_XmlHttpSendToShow");
	var xmlHttp;

	var self = this;	
	
	self.handleFunctionProcess = smc_ProcessResponse;

	try {	
		if (window.ActiveXObject) {
			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		} else if (window.XMLHttpRequest) {
					xmlHttp = new XMLHttpRequest();
				}
		//alert ("method");
		
		if( noCache ){
			url = convertUrlNoCache(url) ;			
		}
		//alert (url);
		if (method == 'POST'){

			xmlHttp.open("POST", url, true);
			xmlHttp.onreadystatechange = function( ) {
				//logger("AJAXRequest Handler: State =  " + self.AJAX.readyState);
				self.handleFunctionProcess(xmlHttp,nameFunctionProcess,show);
			};
		    //xmlhttp.setRequestHeader("Content-Type", "text/html");
			xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");
			//xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=utf-8");
			//xmlhttp.setRequestHeader('Accept','message/x-jl-formresult')

			//xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8") 
			xmlHttp.send(params);
		} else { // GET
			var sep = (-1 < url.indexOf("?")) ? "&" : "?"	
			var queryString = url + sep ;			
			queryString = queryString + params;
			//alert (queryString);
			//xmlHttp.onreadystatechange = handleFunctionProcess;

			xmlHttp.onreadystatechange = function( ) {
				//logger("AJAXRequest Handler: State =  " + self.AJAX.readyState);
				self.handleFunctionProcess(xmlHttp,nameFunctionProcess,show);
			};
			xmlHttp.open("GET", queryString, true);
			xmlHttp.send(null);
			
		}
				
	} catch (e) {
		alert(e.description);
	}	
	return xmlHttp;
}

function smc_XmlHttpSendToShow(url,method,params,nameFunctionShowResponseText,noCache) {
	return smc_XmlHttpRequest(url,method,params,nameFunctionShowResponseText,noCache,true) ;
}


function smc_ProcessResponse(xmlHttp,nameFunctionProcessResponse,show){
	//alert ("smc_ProcessResponse");
	if(xmlHttp.readyState == 4) {
		// OK
		if(xmlHttp.status == 200) {
			//alert (xmlHttp.responseText);
			if(show){
				var responseText  = xmlHttp.responseText;
				eval(nameFunctionShowResponseText + '(responseText)');
			}
			else eval(nameFunctionProcessResponse + '(xmlHttp)');
		}
	}		
}




function smc_ShowResult(xmlHttpResult,divNameShowError,divNameShowResult,urlGoto){
	try {
		//var responseText = new String() ;
		var responseText  = xmlHttpResult.responseText;
		if(debugAjax == true)alert(responseText);			
		var response  = xmlHttpResult.responseXML.documentElement;
		var errorCount   =	response.getElementsByTagName('errorCount')[0].firstChild.data;		
		//response.getElementsByTagName('data')[0].firstChild.data;
		//alert(errorCount);
		if( parseInt(errorCount) != 0){
			//alert(errorCount);
			var errorMessage   =	response.getElementsByTagName('errorMessage')[0].firstChild.data;
			//alert(errorMessage);
			setInnerById(divNameShowError, errorMessage);					
		}
		else{
			if(urlGoto !="")window.location =urlGoto;
			else {
				var contents = response.getElementsByTagName('data')[0].firstChild.data;
				setInnerById(divNameShowResult, contents);			
			}
		}
	} catch (e) {
		alert("Error show result : " + e.description);
		//setInnerById(divNameShowError, responseText);		
	}
} 	
	

function convertUrlNoCache(url){
	var sep = (-1 < url.indexOf("?")) ? "&" : "?"	
	var newurl = url + sep + "__=" + encodeURIComponent((new Date()).toString());	
	return newurl;
}

function createQueryString(arg) {
	var result = '';
	for (var i = 0; i < arg.length; i++){
		result += '&argument[]='+ arg[i];
	}
	return result;
}


function smc_GetRequestOfForm(objForm) {
	var aParams = new Array();    
	for (var i=0 ; i < objForm.elements.length; i++) {
		var sParam = encodeURIComponent(objForm.elements[i].name);
		sParam += "=";
		sParam += encodeURIComponent(objForm.elements[i].value);
		aParams.push(sParam);
	}     
	return aParams.join("&");
}



function handleFunctionProcessSample() {
	try {
		if(xmlHttp.readyState == 4) {
			// OK
			if(xmlHttp.status == 200) {
				//alert (xmlHttp.responseText)
				//response  = xmlHttp.responseXML.documentElement;
				//response  = xmlHttp.responseText;
				setInnerById("divResult", xmlHttp.responseText)			
			}
			else{
				setInnerById("divResult","Not reader.")		
			}
		}		
	} catch (e) {
		alert(e.description);
	}
}

function handleFunctionProcessSample() {
	try {
		if(xmlHttp.readyState == 4) {
			// OK
			if(xmlHttp.status == 200) {
				//alert (xmlHttp.responseText)
				//response  = xmlHttp.responseXML.documentElement;
				//response  = xmlHttp.responseText;
				setInnerById("divResult", xmlHttp.responseText)			
			}
		}		
	} catch (e) {
		alert(e.description);
	}
}